/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Whitemice.ZOGI.GtkForms;
using Gtk;

namespace Whitemice.ZOGI.GtkFolios
{
  public partial class PanelDialog : Gtk.Dialog
  {
    protected EntityStore         panelStore;
    protected ContactSearchDialog contactSearchDialog = null;
    		
		protected Gdk.Color	evenRowBackground;
		protected Gdk.Color	oddRowBackground;
    
    public PanelDialog()
    {
      this.Build();
      
			evenRowBackground = new Gdk.Color(198, 226, 225); /* Slate#1 */
			oddRowBackground = new Gdk.Color(185, 211, 238); /* Slate#2 */
      
      Gtk.TreeViewColumn objectIdColumn = new Gtk.TreeViewColumn ();
		  objectIdColumn.Title = "Object Id";
		  Gtk.CellRendererText objectIdCell = new Gtk.CellRendererText ();
		  objectIdColumn.PackStart (objectIdCell, true);
      objectIdColumn.SetCellDataFunc (objectIdCell, new Gtk.TreeCellDataFunc (RenderObjectId));
    
		  Gtk.TreeViewColumn descriptionColumn = new Gtk.TreeViewColumn ();
		  descriptionColumn.Title = "Description";
		  Gtk.CellRendererText descriptionCell = new Gtk.CellRendererText ();
		  descriptionColumn.PackStart (descriptionCell, true);
		  descriptionColumn.SetCellDataFunc (descriptionCell, new Gtk.TreeCellDataFunc (RenderDescription));
    
      panelStore = new Whitemice.ZOGI.GtkWidgets.EntityStore();
      
      panelList.Model = panelStore;
      panelList.AppendColumn(objectIdColumn);
      panelList.AppendColumn(descriptionColumn);
      FillModel();
    } /* end ctor */
    
    protected void FillModel()
    {
      panelStore.Clear();
      foreach(Entity e in Server.Connection.UserProfile.CalendarPanel)
        panelStore.AppendValues(e);
    }
     
    protected void RenderObjectId(Gtk.TreeViewColumn column, 
                                  Gtk.CellRenderer cell, 
                                  Gtk.TreeModel model, 
                                  Gtk.TreeIter iter)
    {
	    Entity e = (Whitemice.ZOGI.Entity) model.GetValue (iter, 0);
	    (cell as Gtk.CellRendererText).Text = e.ObjectId.ToString();
	    CellEffect(model, iter, (cell as Gtk.CellRendererText));
    } /* end RenderObjectId */

    protected void RenderDescription(Gtk.TreeViewColumn column, 
                                     Gtk.CellRenderer cell, 
                                     Gtk.TreeModel model, 
                                     Gtk.TreeIter iter)
    {
	    Entity e = (Whitemice.ZOGI.Entity) model.GetValue (iter, 0);
	    if (e is Whitemice.ZOGI.Contact)
	    {
	      Whitemice.ZOGI.Contact c;
	      c = (e as Whitemice.ZOGI.Contact);
	      (cell as Gtk.CellRendererText).Text = string.Format("{0}, {1} (2)", 
	                                                          c.LastName, 
	                                                          c.FirstName, 
	                                                          c.Login);
	    } else if (e is Whitemice.ZOGI.Team)
	      {
	        Whitemice.ZOGI.Team t;
	        t = (e as Whitemice.ZOGI.Team);
  	      (cell as Gtk.CellRendererText).Text = string.Format("{0}", t.Name);
	      } else if (e is Whitemice.ZOGI.Resource)
	        {
	          Whitemice.ZOGI.Resource r;
	          r = (e as Whitemice.ZOGI.Resource);
	          (cell as Gtk.CellRendererText).Text = string.Format("{0}", r.Name);
	        }
	    CellEffect(model, iter, (cell as Gtk.CellRendererText));
    } /* end RenderDescription */
    
    protected virtual void CellEffect(Gtk.TreeModel model, 
			                                Gtk.TreeIter iter,
			                                Gtk.CellRendererText cell)
    {
			if((model.GetPath(iter).Indices[0] % 2) == 1)
				cell.BackgroundGdk = oddRowBackground;
				else cell.BackgroundGdk = evenRowBackground;
    } /* end CellEffect */

    protected virtual void OnOkClicked (object sender, System.EventArgs e)
    {
      Server.Connection.UserProfile.ClearPanel();
      //foreach(Whitemice.ZOGI.Entity en in panelStore.Entities)
       // Server.Connection.UserProfile.AddToPanel(en);
      Server.Connection.UserProfile.Save();        
      this.Hide();
    }

    protected virtual void OnCancelClicked (object sender, System.EventArgs e)
    {
      this.Hide();
    }

    protected virtual void OnRemoveClicked (object sender, System.EventArgs e)
    {
			TreeIter 			i;
			TreeModel 		m;
			Entity			  en;

			en = null;
			if(panelList.Selection.GetSelected(out m, out i))
			{
				en = (panelStore.GetValue (i, 0) as Whitemice.ZOGI.Entity);
				panelStore.Remove(en);
		  }
    } /* end OnRemoveClicked */

    protected virtual void OnAddResourceClicked (object sender, System.EventArgs e)
    {
    }

    protected virtual void OnAddTeamClicked (object sender, System.EventArgs e)
    {
    }

    protected virtual void OnAddContactClicked (object sender, System.EventArgs e)
    {
      if (contactSearchDialog == null)
      {
        contactSearchDialog = new ContactSearchDialog(
                                    ZOGIUtility.GetWindow(this),
                                    0,
                                    "Add Contact To Panel");
        contactSearchDialog.SetContactId +=
          new ContactSearchDialog.SetContactIdHandler(AddEntityToPanel);
      }
      contactSearchDialog.Present();    
    } /* end OnAddContactClicked */
    
    public virtual void AddEntityToPanel(int _objectId)
    {
      Whitemice.ZOGI.Entity entity;
      entity = Server.Connection.Get(_objectId, 65535, true);
      if (entity == null)
      {
      } else if ((entity is Whitemice.ZOGI.Contact) ||
                 (entity is Whitemice.ZOGI.Team) ||
                 (entity is Whitemice.ZOGI.Resource))
        {
          panelStore.AppendValues(entity);
        }    
    } /* end AddEntityToPanel */
  }
}
